package com.allwork.myshares;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;


import com.allwork.myshares.baseclass.BaseActivity;
import com.allwork.myshares.forumworks.ForumFragment;
import com.allwork.myshares.marketworks.MarketFragment;
import com.allwork.myshares.newsworks.NewsFragment;
import com.allwork.myshares.optionalworks.OptionalFragment;

public class MainActivity extends BaseActivity {
    private TabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        tabHost = bindView(android.R.id.tabhost);
    }

    @Override
    protected void initData() {

        tabHost.setup();

        TabHost.TabSpec specNews = tabHost.newTabSpec("news");
        View viewNews = getLayoutInflater().inflate(R.layout.tabspec_news, null);
        specNews.setIndicator(viewNews);
        specNews.setContent(R.id.view1);
        tabHost.addTab(specNews);

        TabHost.TabSpec specMarket = tabHost.newTabSpec("market");
        View viewMarkets = getLayoutInflater().inflate(R.layout.tabspec_market, null);
        specMarket.setIndicator(viewMarkets);
        specMarket.setContent(R.id.view2);
        tabHost.addTab(specMarket);

        TabHost.TabSpec specForum = tabHost.newTabSpec("forum");
        View viewForum = getLayoutInflater().inflate(R.layout.tabspec_forum, null);
        specForum.setIndicator(viewForum);
        specForum.setContent(R.id.view3);
        tabHost.addTab(specForum);

        TabHost.TabSpec specOptional = tabHost.newTabSpec("optional");
        View viewOptional = getLayoutInflater().inflate(R.layout.tabspec_optional, null);
        specOptional.setIndicator(viewOptional);
        specOptional.setContent(R.id.view4);
        tabHost.addTab(specOptional);
        tabHost.getTabWidget().setDividerDrawable(null);
        //fragment 管理器 替换方法
        useFragmentManager();
    }

    private void useFragmentManager() {
        android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.view1, new NewsFragment());
        transaction.replace(R.id.view2, new MarketFragment());
        transaction.replace(R.id.view3, new ForumFragment());
        transaction.replace(R.id.view4, new OptionalFragment());
        transaction.commit();

    }

}
